package org.tao.lightningmq.broker.utils;

import org.tao.lightningmq.broker.cache.CommonCache;
import org.tao.lightningmq.common.constants.BrokerConstants;

/**
 * @Author lht
 * @date 2025/1/3 - 21:23
 * @description: 构建commitLog的工具类
 */
public class LogFileNameUtil {

    public static String getFirstCommitLogName() {
        return "00000000";
    }

    public static String getFirstConsumeQueueName() {
        return "00000000";
    }

    /**
     * 构建commitLog文件路径
     * @param topic topic名称
     * @param CommitLogFileName commitLog文件名称
     * @return commitLog文件路径
     */
    public static String buildCommitLogFilePath(String topic, String CommitLogFileName) {
        return buildCommitLogBasePath(topic)
                + BrokerConstants.SPLIT
                + CommitLogFileName;
    }

    /**
     * 构建commitLog文件基础路径
     * @param topic topic名称
     * @return commitLog所在目录的基本路径
     */
    public static String buildCommitLogBasePath(String topic) {
        return CommonCache.getGlobalProperties().getMqHome()
                + BrokerConstants.BASE_COMMIT_PATH
                + topic;
    }

    /**
     * 构建consumeQueue文件路径
     * @param topic topic名称
     * @param queueId 队列id
     * @param consumeQueueFileName consumeQueue文件名称
     * @return consumeQueue文件路径
     */
    public static String buildConsumeQueueFilePath(String topic, Integer queueId, String consumeQueueFileName) {
        return buildConsumeQueueBasePath(topic)
                + BrokerConstants.SPLIT
                + queueId
                + BrokerConstants.SPLIT
                + consumeQueueFileName;
    }

    /**
     * 构建consumeQueue文件基础路径
     * @param topic topic名称
     * @return consumeQueue所在目录的基本路径
     */
    public static String buildConsumeQueueBasePath(String topic) {
        return CommonCache.getGlobalProperties().getMqHome()
                + BrokerConstants.BASE_CONSUME_QUEUE_PATH
                + topic;
    }

    /**
     * 递增ConsumeQueue文件名
     * @param oldFileName 旧文件名
     * @return 新文件名
     */
    public static String incrConsumeQueueFileName(String oldFileName) {
        return incrCommitLogFileName(oldFileName);
    }

    /**
     * 递增consumeQueue文件名
     * @param oldFileName 旧文件名
     * @return 新文件名
     */
    private static String incrCommitLogFileName(String oldFileName) {
        if (oldFileName.length() != 8) {
            throw new IllegalArgumentException("FileName length must be 8");
        }
        long fileIdx = Long.parseLong(oldFileName);
        if (fileIdx < 0 || fileIdx > 99999999) {
            throw new IllegalArgumentException("FileName must be between 00000000 and 99999999");
        }
        String newFileName = String.valueOf(++fileIdx);
        int needAppendLen = 8 - newFileName.length();
        return String.format("%0" + needAppendLen + "d", 0) + newFileName;
    }

    public static void main(String[] args) {
        assert incrConsumeQueueFileName("00007000").equals("00007001");
        assert incrConsumeQueueFileName("00007099").equals("00007100");
    }

}
